package com.googlecode.particlesys;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.googlecode.particlesys.templates.Burst;
import com.googlecode.particlesys.templates.Cyclone;
import com.googlecode.particlesys.templates.Explosion;
import com.googlecode.particlesys.templates.Fountain;
import com.googlecode.particlesys.templates.Shooter;
import com.googlecode.particlesys.templates.TargetPosition;
import com.googlecode.particlesys.templates.Shuriken;

public class MainExample implements ActionListener, ChangeListener {

	private JFrame frame;
	private JSlider windSlider;
	private JSlider gravitySlider;
	private Simulation sim;
	
	public static void main(String[] args) throws Exception {
		new MainExample();
	}
	
	private MainExample() throws Exception {
		frame = new JFrame("Particle System in Java");
		frame.setSize(1024, 768);
		frame.setResizable(false);
		
		frame.setLayout(null);
		
		JPanel leftPanel = new JPanel();
		leftPanel.setSize(275, frame.getHeight());
		
		JLabel wind = new JLabel("Wind: ");
		JLabel gravity = new JLabel("Gravity: ");
		Hashtable<Integer, JLabel> windLabels = new Hashtable<Integer, JLabel>();
		Hashtable<Integer, JLabel> gravityLabels = new Hashtable<Integer, JLabel>();
		for(int i = -3; i < 4; i++){
			windLabels.put(i, new JLabel(String.valueOf(i)));
			gravityLabels.put(i, new JLabel(String.valueOf(i)));
		}
		JLabel radiusLabel = new JLabel("Radius Draw: ");
		ButtonGroup radiusGroup = new ButtonGroup();
		JRadioButton radiusOn = new JRadioButton("On");
		JRadioButton radiusOff = new JRadioButton("Off");
		radiusOn.addActionListener(this);
		radiusOff.addActionListener(this);
		radiusOff.setSelected(true); //off by default
		radiusGroup.add(radiusOn);
		radiusGroup.add(radiusOff);

		
		windSlider = new JSlider(-3, 3, 1); //min, max, value
		gravitySlider = new JSlider(-3, 3, 1); //min, max, value
		windSlider.setLabelTable(windLabels);
		windSlider.setSnapToTicks(true);
		windSlider.setPaintLabels(true);
		gravitySlider.setLabelTable(gravityLabels);
		gravitySlider.setSnapToTicks(true);
		gravitySlider.setPaintLabels(true);
		
		leftPanel.add(wind);
		leftPanel.add(windSlider);
		leftPanel.add(gravity);
		leftPanel.add(gravitySlider);
		leftPanel.add(radiusLabel);
		leftPanel.add(radiusOn);
		leftPanel.add(radiusOff);
		
		frame.add(leftPanel);
		frame.add(this.sim = new Simulation());
		
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.sim.setVisible(true);
		this.sim.init(2);
		
		this.sim.getEmitters().add(new Explosion(100, 100));
		this.sim.getEmitters().add(new Cyclone(300, 300));
		this.sim.getEmitters().add(new TargetPosition(550, 400));
		this.sim.getEmitters().add(new Burst(200, 625));
		this.sim.getEmitters().add(new Fountain(550, 250));
		this.sim.getEmitters().add(new Shuriken(500, 600));
		this.sim.getEmitters().add(new Shooter(400, 500));
		
		while(true){
			this.sim.redraw();
			Thread.sleep(1000/60);
		}
	}

	@Override
	public void stateChanged(ChangeEvent s) {
		//slider moved
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("On")) {
			Simulation.setDisplayingRadius(true);
		} else if(e.getActionCommand().equals("Off")) {
			Simulation.setDisplayingRadius(false);
		}
	}
}
